package com.zhouchb.blog.Controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhouchb.blog.bean.Tag;
import com.zhouchb.blog.service.TagsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author Zhouchb
 * @Create 2021-09-2021/9/5 :22:29
 * @Description
 */
@Slf4j
@Controller
@RequestMapping("/admin")
public class TagsController {

    @Autowired
    private TagsService tagesService;

    @GetMapping("/tags")
    public String tags(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        PageHelper.startPage(pn,6);
        List<Tag> tags = tagesService.listTagService();
        PageInfo<Tag> tagPageInfo = new PageInfo<>(tags, 5);
        model.addAttribute("tagPageInfo",tagPageInfo);
        return "admin/tags";
    }
    //跳转到新增
    @GetMapping("/tags/tags_release")
    public String tags_release(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags_release";
    }
    //新增提交
    @PostMapping("/tags")
    @Transactional
    public String typesPost(@Valid Tag tag, BindingResult binding, RedirectAttributes redirectAttributes){
        Tag tagByNameService = tagesService.getTagByNameService(tag.getName());
        if (tagByNameService != null){
            binding.rejectValue("name","Error","彬哥类型名已经存在了");
        }
        if (binding.hasErrors()){
            return "admin/tags_release";
        }
        int i = tagesService.saveTagesService(tag);

        if (i>0){
            redirectAttributes.addFlashAttribute("TagMessage", "恭喜恭喜！标签添加成功！");
        }else {
            redirectAttributes.addFlashAttribute("FailTagMessage", "哎呀呀！添加标签失败了呀！");
        }
        return "redirect:/admin/tags";
    }

    //tag编辑
    @GetMapping("/tags/{id}/input")
    public String editTag(@PathVariable("id")Long id,Model model){
        log.info("输出：{}",tagesService.getTagByIdService(id));
        model.addAttribute("tag",tagesService.getTagByIdService(id));
        return "admin/tags_release";
    }
    //tag编辑提交
    @PostMapping("/tags/{id}")
    public String postUpdateTags(@Valid Tag tag, BindingResult bindingResult,@PathVariable("id") Long id ,RedirectAttributes redirectAttributes){
        log.info(tag.getName());
        Tag tagByNameService = tagesService.getTagByNameService(tag.getName());
        if (tagByNameService != null){
            bindingResult.rejectValue("name","Error","彬哥标签名已经存在了");
        }
        if(bindingResult.hasErrors()){
            return "admin/tags_release";
        }
        int i = tagesService.updateTagService(id,tag);
        if (i>0){
            redirectAttributes.addFlashAttribute("TagMessage","恭喜恭喜！标签更新成功！！");
        }else {
            redirectAttributes.addFlashAttribute("FailTagMessage","哎呀呀，又更新失败了！！");
        }
        return "redirect:/admin/tags";
    }
//删除tag
    @ResponseBody
    @GetMapping("/tag/del/{id}")
    public String delTag(@PathVariable("id")Long id){
        int i = tagesService.delTagService(id);
        if (i>0){
            return "删除标签成功";
        }else {
            return "删除标签成功";
        }
    }
}

